<?php
declare (strict_types=1);

namespace app\business\controller;

use app\common\model\VideoCate;
use Qiniu\Auth;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;
use app\common\model\Product;
use app\common\model\Business;

class Video extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\BusinessVideo';

    protected $hasOne = ['product'];

    //额外查询条件
    protected $extraSearch = ['business_id'];

    public function index()
    {
        $extra_map = ['business_id'=>$this->admin['business_id']];
        return $this->setTable($extra_map);
    }

    //自动构建table,按需修改
    public function setTable($extra_map=[])
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'introduce', 'title' => '介绍'])
            ->addRow(['field' => 'total_hit', 'title' => '点赞量', 'width' => 110])
            ->addRow(['field' => 'total_comment', 'title' => '评论量', 'width' => 110])
            ->addRow(['field' => 'product', 'title' => '关联产品', 'width' => 110, 'templet' => '<div>{{d.product?d.product.title:"--"}}</div>'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1600, 'height' => 1200], 'delete'])
            ->addTopButtons(['add' => ['width' => 1200, 'height' => 1000], 'deleteAll'])
            ->setSearch($this->setSearch())
            ->addSwitchTool([['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭']])
            ->addExtraMap($extra_map)
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['introduce', '介绍', 'input', '', '', '', 'like']);

    }

    public function add($name = '', $tpl = 'video/edit')
    {
        if(Business::where('id', $this->admin['business_id'])->value('video_status') != 2){
            exit('请在APP端完成上传视频申请再进行上传！');
        }
        $data = $this->assignData();
        View::assign($data);
        return parent::add($name, $tpl);
    }

    public function edit($name = '', $id = '', $tpl = 'video/edit', $info = '')
    {
        $data = $this->assignData();
        View::assign($data);
        $info = $this->model::find($id);
        $info->video_link = $info->link;
        $info->video_link_url = $data['domain'].$info->link;
        return parent::edit($name, $id, $tpl, $info);
    }

    public function assignData()
    {
        $config = config('qiniu');
        // 构建鉴权对象
        $auth = new Auth($config['accessKey'], $config['secretKey']);
        $token = $auth->uploadToken($config['bucket']);

        return [
            'token' => $token,
            'domain' => $config['domain'],
            'cate_list' => VideoCate::where(['is_del' => 0, 'status' => 1])->select(),
            'product_list' => Product::where(['is_del' => 0, 'status' => 1, 'cate_1'=>7, 'business_id'=>$this->admin['business_id']])->select(),
        ];
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
